<?php

namespace support;

use support\Log;

/**
 * 自定义助手函数
 */

class customHelpers
{

    /**
     * 生成下载链接
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2025-01-23
     * @param array $arr
     * @return array
     */
    public static function assembleUrlArray(array $arr): array
    {
        // 项目名称
        $projectName = isset($arr['project_name']) ? $arr['project_name'] : '';
        $baseLocalPath = public_path() . '/' . 'download' . '/' . $projectName;
        $url = [];
        // 循环项目中会议
        foreach ($arr['projectMeeting'] as $k => $v) {
            // 路径拼接会议名称
            $meetingLocalPath = $baseLocalPath . '/' . $v['meeting_name'];
            // 循环项目中受邀讲者
            foreach ($v['projectMeetingInvitationSpeaker'] as $kk => $vv) {
                // 路径拼接讲者名称
                $speakerLocalPath = $meetingLocalPath . '/' . $vv['speaker_name'];

                // 上传资料 start
                if (!empty($vv['uploadData'])) {
                    if (!empty($vv['uploadData']['meeting_materiel'])) {
                        $meeting_materiel = json_decode($vv['uploadData']['meeting_materiel'], true);

                        foreach ($meeting_materiel as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' => $speakerLocalPath . '/' . 'uploadData' . '/' . 'meeting_materiel' . '/'
                                ],
                            );
                        }
                    }
                    if (!empty($vv['uploadData']['meeting_screen_recording'])) {
                        $meeting_screen_recording = json_decode($vv['uploadData']['meeting_screen_recording'], true);
                        foreach ($meeting_screen_recording as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'uploadData' . '/' . 'meeting_screen_recording' . '/'
                                ],
                            );
                        }
                    }
                    if (!empty($vv['uploadData']['meeting_photo'])) {
                        $meeting_photo = json_decode($vv['uploadData']['meeting_photo'], true);
                        foreach ($meeting_photo as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'uploadData' . '/' . 'meeting_photo' . '/'
                                ],
                            );
                        }
                    }
                } //上传资料 end
                // 讲者简历 start
                if (!empty($vv['speakerResume'])) {
                    if (!empty($vv['speakerResume']['speaker_resume'])) {
                        $speaker_resume = json_decode($vv['speakerResume']['speaker_resume'], true);
                        foreach ($speaker_resume as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'speakerResume' . '/' . 'speaker_resume' . '/'
                                ],
                            );
                        }
                    }
                    if (!empty($vv['speakerResume']['speaker_resume_translate'])) {
                        $speaker_resume_translate = json_decode($vv['speakerResume']['speaker_resume_translate'], true);
                        foreach ($speaker_resume_translate as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'speakerResume' . '/' . 'speaker_resume_translate' . '/'
                                ],
                            );
                        }
                    }
                    if (!empty($vv['speakerResume']['speaker_avatar'])) {
                        $speaker_avatar = json_decode($vv['speakerResume']['speaker_avatar'], true);
                        foreach ($speaker_avatar as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'speakerResume' . '/' . 'speaker_avatar' . '/'
                                ]
                            );
                        }
                    }
                } // 讲者简历 end
                // // 讲者护照 start
                if (!empty($vv['speakerPassport'])) {
                    if (!empty($vv['speakerPassport']['remittance'])) {
                        $remittance = json_decode($vv['speakerPassport']['remittance'], true);
                        foreach ($remittance as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'speakerPassport' . '/' . 'remittance' . '/'
                                ]
                            );
                        }
                    }
                    if (!empty($vv['speakerPassport']['upload_passport'])) {
                        $upload_passport = json_decode($vv['speakerPassport']['upload_passport'], true);
                        foreach ($upload_passport as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'speakerPassport' . '/' . 'upload_passport' . '/'
                                ]
                            );
                        }
                    }
                } // 讲者护照 end
                // 签订协议 start
                if (!empty($vv['signAgreement'])) {
                    if (!empty($vv['signAgreement']['sign_agreement_url'])) {
                        $sign_agreement_url = json_decode($vv['signAgreement']['sign_agreement_url'], true);
                        foreach ($sign_agreement_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'signAgreement' . '/' . 'sign_agreement_url' . '/'
                                ]
                            );
                        }
                    }
                } // 签订协议 end
                // ppt跟进 start
                if (!empty($vv['pptFollowUp'])) {
                    if (!empty($vv['pptFollowUp']['ppt_follow_up'])) {
                        $ppt_follow_up_url = json_decode($vv['pptFollowUp']['ppt_follow_up'], true);
                        foreach ($ppt_follow_up_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'pptFollowUp' . '/' . 'ppt_follow_up' . '/'
                                ]
                            );
                        }
                    }
                } // ppt跟进 end
                // 讲者ppt翻译 start
                if (!empty($vv['pptTranslate'])) {
                    if (!empty($vv['pptTranslate']['ppt_translate'])) {
                        $ppt_translate_url = json_decode($vv['pptTranslate']['ppt_translate'], true);
                        foreach ($ppt_translate_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'pptTranslate' . '/' . 'ppt_translate' . '/'
                                ]
                            );
                        }
                    }
                } // 讲者ppt翻译 end
                // 翻译讨论问题给到讲者 start
                if (!empty($vv['translateToLecturer'])) {
                    if (!empty($vv['translateToLecturer']['ppt_translate_problem'])) {
                        $ppt_translate_problem = json_decode($vv['translateToLecturer']['ppt_translate_problem'], true);
                        foreach ($ppt_translate_problem as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'translateToLecturer' . '/' . 'ppt_translate_problem' . '/'
                                ]
                            );
                        }
                    }
                } // 翻译讨论问题给到讲者 end
                // 可编辑日程 start
                if (!empty($vv['meetingSchedule'])) {
                    if (!empty($vv['meetingSchedule']['schedule_url'])) {
                        $schedule_url = json_decode($vv['meetingSchedule']['schedule_url'], true);
                        foreach ($schedule_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'meetingSchedule' . '/' . 'schedule_url' . '/'
                                ]
                            );
                        }
                    }
                } // 可编辑日程 end
                // 讲者签证包 start
                if (!empty($vv['visaPackage'])) {
                    if (!empty($vv['visaPackage']['visa_url'])) {
                        $visa_url = json_decode($vv['visaPackage']['visa_url'], true);
                        foreach ($visa_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'visaPackage' . '/' . 'visa_url' . '/'
                                ]
                            );
                        }
                    }
                } //讲者签证包 end
                // 讲者邀请函 start
                if (!empty($vv['invitationLetter'])) {
                    if (!empty($vv['invitationLetter']['letter_url'])) {
                        $letter_url = json_decode($vv['invitationLetter']['letter_url'], true);
                        foreach ($letter_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'invitationLetter' . '/' . 'letter_url' . '/'
                                ]
                            );
                        }
                    }
                } // 讲者邀请函 end
                // 调查问卷 start
                if (!empty($vv['meetingQuestions'])) {
                    if (!empty($vv['meetingQuestions']['questionnaire_url'])) {
                        $questions_url = json_decode($vv['meetingQuestions']['questionnaire_url'], true);
                        foreach ($questions_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'meetingQuestions' . '/' . 'questionnaire_url' . '/'
                                ]
                            );
                        }
                    }
                } // 调查问卷 end
                // 最终行程 start
                if (!empty($vv['finalTravel'])) {
                    if (!empty($vv['finalTravel']['final_travel'])) {
                        $final_travel = json_decode($vv['finalTravel']['final_travel'], true);
                        foreach ($final_travel as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'finalTravel' . '/' . 'final_travel' . '/'
                                ]
                            );
                        }
                    }
                    if (!empty($vv['finalTravel']['ticket'])) {
                        $ticket = json_decode($vv['finalTravel']['ticket'], true);
                        foreach ($ticket as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'finalTravel' . '/' . 'ticket' . '/'
                                ]
                            );
                        }
                    }
                    if (!empty($vv['finalTravel']['hotel'])) {
                        $hotel = json_decode($vv['finalTravel']['hotel'], true);
                        foreach ($hotel as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'finalTravel' . '/' . 'hotel' . '/'
                                ]
                            );
                        }
                    }
                } // 最终行程 end
                // 中国签证扫描页 start
                if (!empty($vv['chinaVsiaScan'])) {
                    if (!empty($vv['chinaVsiaScan']['scan_url'])) {
                        $scan_url = json_decode($vv['chinaVsiaScan']['scan_url'], true);
                        foreach ($scan_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'chinaVsiaScan' . '/' . 'scan_url' . '/'
                                ]
                            );
                        }
                    }
                } // 中国签证扫描页 end
                //受邀讲者 活动感谢信 询问报销 出入境章扫描件
                if (!empty($vv['scan'])) {
                    if (!empty($vv['scan']['scan'])) {
                        $scan = json_decode($vv['scan']['scan'], true);
                        foreach ($scan as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'scan' . '/' . 'scan' . '/'
                                ]
                            );
                        }
                    }
                }
                // 报销单据 start
                if (!empty($vv['reimbursement'])) {
                    if (!empty($vv['reimbursement']['reimbursement_url'])) {
                        $reimbursement_url = json_decode($vv['reimbursement']['reimbursement_url'], true);
                        foreach ($reimbursement_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'reimbursement' . '/' . 'reimbursement_url' . '/'
                                ]
                            );
                        }
                    }
                }
                // 结算单
                if (!empty($vv['statement'])) {
                    if (!empty($vv['statement']['final_statement_url'])) {
                        $final_statement_url = json_decode($vv['statement']['final_statement_url'], true);
                        foreach ($final_statement_url as $vk => $vkk) {
                            array_push(
                                $url,
                                [
                                    'remoteFile' => $vkk['url'],
                                    'localFile' =>  $speakerLocalPath . '/' . 'statement' . '/' . 'final_statement_url' . '/'
                                ]
                            );
                        }
                    }
                }
            } // 循环结尾

        }
        return $url;
    }
    public static function getUrl(array $arr): array
    {

        Log::info('保存数据 === 会议 ===' . json_encode($arr['projectMeeting']));
        // 项目名称
        $projectName = isset($arr['project_name']) ? $arr['project_name'] : '';
        $url = [];
        // 项目
        foreach ($arr as $key => $val) {
            // Log::info('保存数据 val ：' . json_encode($val));
            // 循环会议
            foreach ($val['projectMeeting'] as $kk => $vv) {
                // Log::info('保存数据 项目会议列表 vv ：' . json_encode($vv));
                // vv=单场会议
                // 循环会议内的讲者
                foreach ($vv['projectMeetingInvitationSpeaker'] as $kkk => $vvv) {
                    // Log::info('保存数据 讲者信息 vvv ：' . json_encode($vvv));
                    // vvv = 多个讲者
                    // 上传资料
                    if (isset($vvv['uploadData'])) {
                        $meeting_materiel = json_decode($vvv['uploadData']['meeting_materiel'], true);
                        $meeting_screen_recording = json_decode($vvv['uploadData']['meeting_screen_recording'], true);
                        $meeting_photo = json_decode($vvv['uploadData']['meeting_photo'], true);
                        array_push(
                            $url,
                            $meeting_materiel[0]['url'],
                            $meeting_screen_recording[0]['url'],
                            $meeting_photo[0]['url'],
                        );
                    }
                    // 讲者简历
                    if (isset($vvv['speakerResume'])) {
                        $speaker_resume = json_decode($vvv['speakerResume']['speaker_resume'], true);
                        $speaker_resume_translate = json_decode($vvv['speakerResume']['speaker_resume_translate'], true);
                        $speaker_avatar = json_decode($vvv['speakerResume']['speaker_avatar'], true);
                        array_push(
                            $url,
                            $speaker_resume[0]['url'],
                            $speaker_resume_translate[0]['url'],
                            $speaker_avatar[0]['url']
                        );
                    }
                    // 讲者护照
                    if (isset($vvv['speakerPassport'])) {
                        $remittance = json_decode($vvv['speakerPassport']['remittance'], true);
                        $upload_passport = json_decode($vvv['speakerPassport']['upload_passport'], true);
                        $remittance_url = isset($remittance[0]['url']) ? $remittance[0]['url'] : '';
                        $upload_passport_url = isset($upload_passport[0]['url']) ? $upload_passport[0]['url'] : '';
                        // var_dump($upload_passport_url);
                        // die;
                        array_push(
                            $url,
                            $remittance_url,
                            $upload_passport_url,
                        );
                    }

                    // 签订协议
                    if (isset($vvv['signAgreement'])) {
                        $sign_agreement_url = json_decode($vvv['signAgreement']['sign_agreement_url'], true);
                        array_push(
                            $url,
                            $sign_agreement_url[0]['url'],
                        );
                    }

                    // PPT跟进
                    if (isset($vvv['pptFollowUp'])) {
                        $ppt_follow_up = json_decode($vvv['pptFollowUp']['ppt_follow_up'], true);
                        array_push(
                            $url,
                            $ppt_follow_up[0]['url'],
                        );
                    }

                    // PPT翻译
                    if (isset($vvv['pptTranslate'])) {
                        $ppt_translate = json_decode($vvv['pptTranslate']['ppt_translate'], true);
                        array_push(
                            $url,
                            $ppt_translate[0]['url'],
                        );
                    }

                    // PPT翻译问题给到讲者
                    if (isset($vvv['translateToLecturer'])) {
                        $ppt_translate_problem = json_decode($vvv['translateToLecturer']['ppt_translate_problem'], true);
                        array_push(
                            $url,
                            $ppt_translate_problem[0]['url'],
                        );
                    }

                    // 会议受邀讲者可编辑日程
                    if (isset($vvv['meetingSchedule'])) {
                        $schedule_url = json_decode($vvv['meetingSchedule']['schedule_url'], true);
                        array_push(
                            $url,
                            $schedule_url[0]['url'],
                        );
                    }

                    // 受邀讲者签证包
                    if (isset($vvv['visaPackage'])) {
                        $visa_url = json_decode($vvv['visaPackage']['visa_url'], true);
                        array_push(
                            $url,
                            $visa_url[0]['url'],
                        );
                    }

                    // 邀请函
                    if (isset($vvv['invitationLetter'])) {
                        $letter_url = json_decode($vvv['invitationLetter']['letter_url'], true);
                        array_push(
                            $url,
                            $letter_url[0]['url'],
                        );
                    }

                    // 喜好问卷
                    if (isset($vvv['meetingQuestions'])) {
                        $questionnaire_url = json_decode($vvv['meetingQuestions']['questionnaire_url'], true);
                        array_push(
                            $url,
                            $questionnaire_url[0]['url'],
                        );
                    }

                    // 最终行程
                    if (isset($vvv['finalTravel'])) {
                        $final_travel = json_decode($vvv['finalTravel']['final_travel'], true);
                        $ticket = json_decode($vvv['finalTravel']['ticket'], true);
                        $hotel = json_decode($vvv['finalTravel']['hotel'], true);
                        array_push(
                            $url,
                            $final_travel[0]['url'],
                            $ticket[0]['url'],
                            $hotel[0]['url'],
                        );
                    }

                    // 受邀讲者中国签证扫描页
                    if (isset($vvv['chinaVsiaScan'])) {
                        $scan_url = json_decode($vvv['chinaVsiaScan']['scan_url'], true);
                        array_push(
                            $url,
                            $scan_url[0]['url']
                        );
                    }

                    // 受邀讲者中国签证扫描页
                    if (isset($vvv['chinaVsiaScan'])) {
                        $scan_url = json_decode($vvv['chinaVsiaScan']['scan_url'], true);
                        array_push(
                            $url,
                            $scan_url[0]['url']
                        );
                    }

                    // 受邀讲者 活动感谢信 \n询问报销 \n出入境章扫描件
                    if (isset($vvv['scan'])) {
                        $thank_you = json_decode($vvv['scan']['thank_you'], true);
                        $reimbursement = json_decode($vvv['scan']['reimbursement'], true);
                        $scan = json_decode($vvv['scan']['scan'], true);
                        array_push(
                            $url,
                            $thank_you[0]['url'],
                            $reimbursement[0]['url'],
                            $scan[0]['url']
                        );
                    }
                    // 索要报销单据扫描件
                    if (isset($vvv['reimbursement'])) {
                        $reimbursement_url = json_decode($vvv['reimbursement']['reimbursement_url'], true);
                        array_push(
                            $url,
                            $reimbursement_url[0]['url']
                        );
                    }

                    // 结算单
                    if (isset($vvv['statement'])) {
                        $final_statement_url = json_decode($vvv['statement']['final_statement_url'], true);
                        array_push(
                            $url,
                            $final_statement_url[0]['url']
                        );
                    }
                }
            }
        }


        Log::info('保存数据 最终URL数组' . json_encode($url) . ' ====== ' . count($url) . ' ======');

        return $url;
    }
}
